unit POManagement;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, RzTabs, AdvGlowButton, RzPanel, RzButton, ExtCtrls, StdCtrls,
  Mask, RzEdit, RzCmboBx, RzLabel, RzRadChk, DB, ADODB, RzLstBox,
  AdvNavBar, RzDBChk, RzDBCmbo, Grids, BaseGrid, AdvGrid, DBAdvGrd,
  DBCtrls, RzDBEdit, Menus,gtExProPDFDoc, gtPDFCrypt, gtClasses,
  gtCstPDFDoc, gtExPDFDoc, gtPDFDoc,QRPDFFilt,ComObj,ShellAPI;

type
  TfrmPOManagement = class(TForm)
    PageMainMenu: TRzPageControl;
    TabSheet4: TRzTabSheet;
    RzPageControl1: TRzPageControl;
    TabSheet1: TRzTabSheet;
    RzPanel2: TRzPanel;
    RzBitBtn4: TRzBitBtn;
    RzBitBtn1: TRzBitBtn;
    RzPanel3: TRzPanel;
    RzPanel4: TRzPanel;
    AdvGlowButton4: TAdvGlowButton;
    AdvGlowButton5: TAdvGlowButton;
    AdvGlowButton6: TAdvGlowButton;
    RzLabel1: TRzLabel;
    RzComboBox1: TRzComboBox;
    RzLabel2: TRzLabel;
    RzEdit1: TRzEdit;
    RzCheckBox1: TRzCheckBox;
    RzDateTimeEdit1: TRzDateTimeEdit;
    RzLabel3: TRzLabel;
    RzDateTimeEdit2: TRzDateTimeEdit;
    RzLabel4: TRzLabel;
    RzBitBtn5: TRzBitBtn;
    TabSheet2: TRzTabSheet;
    RzPanel5: TRzPanel;
    RzBitBtn6: TRzBitBtn;
    RzBitBtn7: TRzBitBtn;
    RzPanel6: TRzPanel;
    RzLabel5: TRzLabel;
    RzLabel6: TRzLabel;
    RzLabel7: TRzLabel;
    RzLabel8: TRzLabel;
    RzComboBox2: TRzComboBox;
    RzEdit2: TRzEdit;
    RzCheckBox2: TRzCheckBox;
    RzDateTimeEdit3: TRzDateTimeEdit;
    RzDateTimeEdit4: TRzDateTimeEdit;
    RzPanel7: TRzPanel;
    AdvGlowButton1: TAdvGlowButton;
    AdvGlowButton2: TAdvGlowButton;
    AdvGlowButton3: TAdvGlowButton;
    RzBitBtn8: TRzBitBtn;
    RzBitBtn9: TRzBitBtn;
    RzBitBtn10: TRzBitBtn;
    RzBitBtn11: TRzBitBtn;
    RzBitBtn12: TRzBitBtn;
    RegEntryPage: TRzPageControl;
    RzTabSheet2: TRzTabSheet;
    RzPanel8: TRzPanel;
    RzLabel9: TRzLabel;
    RzLabel10: TRzLabel;
    RzLabel11: TRzLabel;
    RzLabel14: TRzLabel;
    RzLabel19: TRzLabel;
    RzLabel22: TRzLabel;
    RzLabel12: TRzLabel;
    RzLabel13: TRzLabel;
    RzLabel15: TRzLabel;
    fldRebatesKey: TRzDBEdit;
    fldReqFormNo: TRzDBEdit;
    fldRebStartDate: TRzDBDateTimeEdit;
    fldRebEndDate: TRzDBDateTimeEdit;
    fldRebPreparedby: TRzDBLookupComboBox;
    fldRebNotedby: TRzDBLookupComboBox;
    fldRebCheckedby: TRzDBLookupComboBox;
    fldRebApprovedby: TRzDBLookupComboBox;
    RzPanel9: TRzPanel;
    dbgridRequestDetails: TDBAdvStringGrid;
    NavBarTaskMan: TAdvNavBar;
    NavBarPanelSettings: TAdvNavBarPanel;
    RzListBox1: TRzListBox;
    NavBarPanelReports: TAdvNavBarPanel;
    RzListBox2: TRzListBox;
    NavBarPanelTask: TAdvNavBarPanel;
    TaskManTaskList: TRzListBox;
    AdvNavBarPanel1: TAdvNavBarPanel;
    RzPanel12: TRzPanel;
    RzBitBtn13: TRzBitBtn;
    RzBitBtn14: TRzBitBtn;
    TabSheet5: TRzTabSheet;
    ADOConnection1: TADOConnection;
    QryRequest: TADODataSet;
    dsRequest: TDataSource;
    QryRequestDetails: TADODataSet;
    dsRequestDetails: TDataSource;
    RzPanel1: TRzPanel;
    RzLabel16: TRzLabel;
    edtDetailsItem: TRzDBLookupComboBox;
    RzLabel17: TRzLabel;
    RzDBEdit1: TRzDBEdit;
    RzLabel18: TRzLabel;
    RzDBEdit2: TRzDBEdit;
    RzLabel34: TRzLabel;
    RzLabel35: TRzLabel;
    RzDBEdit3: TRzDBEdit;
    QryEmployee: TADODataSet;
    dsEmployee: TDataSource;
    RzDBEdit4: TRzDBEdit;
    QrySupplier: TADODataSet;
    dsSupplier: TDataSource;
    QryCategory: TADODataSet;
    dsCategory: TDataSource;
    QryItem: TADODataSet;
    dsItem: TDataSource;
    RzLabel20: TRzLabel;
    RzDBLookupComboBox3: TRzDBLookupComboBox;
    popMenuReqDetails: TPopupMenu;
    AddNewItem1: TMenuItem;
    ModifyItem1: TMenuItem;
    RemoveItem1: TMenuItem;
    SaveItem1: TMenuItem;
    RzDBEdit5: TRzDBEdit;
    QryRDView: TADODataSet;
    dsRDView: TDataSource;
    TabSheet3: TRzTabSheet;
    dbGridRequestList: TDBAdvStringGrid;
    RzLabel21: TRzLabel;
    RzDBComboBox1: TRzDBComboBox;
    QryRequestList: TADODataSet;
    dsRequestList: TDataSource;
    TabSheet6: TRzTabSheet;
    RzPanel10: TRzPanel;
    RzLabel23: TRzLabel;
    RzLabel24: TRzLabel;
    RzLabel25: TRzLabel;
    RzLabel26: TRzLabel;
    RzLabel27: TRzLabel;
    RzLabel28: TRzLabel;
    RzLabel29: TRzLabel;
    RzLabel30: TRzLabel;
    RzDBEdit6: TRzDBEdit;
    RzDBLookupComboBox1: TRzDBLookupComboBox;
    RzDBEdit9: TRzDBEdit;
    RzDBEdit10: TRzDBEdit;
    RzDBLookupComboBox2: TRzDBLookupComboBox;
    RzDBEdit11: TRzDBEdit;
    RzDBEdit12: TRzDBEdit;
    RzDBEdit13: TRzDBEdit;
    gtPDFDocument1: TgtPDFDocument;
    procedure QryRequestDetailsAfterInsert(DataSet: TDataSet);
    procedure AddNewItem1Click(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure RzBitBtn4Click(Sender: TObject);
    procedure AdvGlowButton5Click(Sender: TObject);
    procedure AdvGlowButton6Click(Sender: TObject);
    procedure SaveItem1Click(Sender: TObject);
    procedure RzBitBtn10Click(Sender: TObject);
    procedure FileFind(Path, FileName, Ext: string);
  private
    { Private declarations }
  public
    { Public declarations }
    iSubDirectory,NewFilename,sCGConnOL,sLocalCEBUGEMSHRIS :String;    
  end;

var
  frmPOManagement: TfrmPOManagement;

implementation

uses Reports;

{$R *.dfm}

procedure TfrmPOManagement.FileFind(Path, FileName, Ext: string);
var
  SearchRec: TSearchRec;
  FindResult: Integer;
  S: string;
begin
  S := Path + '\' + FileName + '.' + Ext;
  FindResult := FindFirst(S, faAnyFile, SearchRec);
  while FindResult = 0 do
  begin
    if ((SearchRec.Name <> '.') and (SearchRec.Name <> '..')) then
      if not ((SearchRec.Attr and faDirectory) > 0) then
      begin
        if not DeleteFile(iSubDirectory + '\' + SearchRec.Name) then
        begin
        end;
      end;
    FindResult := FindNext(SearchRec);
  end;
  SysUtils.FindClose(SearchRec);
end;

procedure TfrmPOManagement.QryRequestDetailsAfterInsert(DataSet: TDataSet);
begin
     QryRequestDetails.FieldByName('REQUESTKEY').AsInteger:=QryRequest.FieldByName('REQUESTKEY').AsInteger;
end;

procedure TfrmPOManagement.AddNewItem1Click(Sender: TObject);
begin
     QryRequestDetails.Append;
     edtDetailsItem.SetFocus;
end;

procedure TfrmPOManagement.FormShow(Sender: TObject);
begin
     QryEmployee.Open;
     QrySupplier.Open;
     QryCategory.Open;
     QryItem.Open;
     QryRequest.Open;

     QryRequestDetails.Open;

     QryRDView.Open;
     QryRequestList.Open;
end;

procedure TfrmPOManagement.RzBitBtn4Click(Sender: TObject);
begin
     QryRequest.Append;
     fldReqFormNo.SetFocus;
end;

procedure TfrmPOManagement.AdvGlowButton5Click(Sender: TObject);
begin
     QryRequest.Post;
end;

procedure TfrmPOManagement.AdvGlowButton6Click(Sender: TObject);
begin
     QryRequest.Delete;
end;

procedure TfrmPOManagement.SaveItem1Click(Sender: TObject);
begin
     QryRequestDetails.Post;
end;

procedure TfrmPOManagement.RzBitBtn10Click(Sender: TObject);
var
AFrmReports:TfrmReports;
repPDF,repPDF2:TQRPDFDocumentFilter;
LDoc: TgtPDFDocument;
NewFilename2,NewFilename3:string;
  pagescount,spage:Integer;
  strSQLPrint:string;
begin

    if dbGridRequestList.RowCount=0 then
     begin
       Application.MessageBox('No Results to print.', 'No Request', MB_ICONEXCLAMATION);
       Exit;
     end;

     AFrmReports:= TfrmReports.Create(nil);
     AFrmReports.ADOConnection1:=ADOConnection1;
     AFrmReports.Mem2IERSource.EmptyTable;
     if not QryRequestList.IsEmpty then
     begin
       QryRequestList.First;
       while not QryRequestList.Eof do begin
         with AFrmReports.Mem2IERSource do
         begin
              Open;
              Append;
              FieldByName('SORTKEY').AsInteger:=QryRequestList.FieldByName('itemkey').AsInteger;
              FieldByName('IEEVALQTEXT').AsString:=QryRequestList.FieldByName('name').AsString;
              FieldByName('TTLX').AsInteger:=QryRequestList.FieldByName('quantity').AsInteger;
              FieldByName('TTLYString').AsString:=QryRequestList.FieldByName('unit').AsString;
              Post;
         end;
         QryRequestList.Next;
       end;
     end;
   
     //if cbEvalResultDR.Checked then
     AFrmReports.QRLTitleDate.Caption:=formatdatetime('yyyy-mm-dd',Now());
     AFrmReports.QrySoniaReport.Connection:=ADOConnection1;
     AFrmReports.QrySoniaReport.Open;
     //AFrmReports.QRLTitleInsName.Caption:=trim(cboInstructor.Text);
    iSubDirectory:=ExtractFilePath(Application.ExeName) + 'Reports';
    FileFind(iSubDirectory,'*','pdf'); // delete existing pdf in the folder.
    NewFilename := '';
    if not DirectoryExists(iSubDirectory) then MkDir(iSubDirectory);
    NewFilename:=iSubDirectory+'\'+CreateClassID+'.pdf';
    repPDF := TQRPDFDocumentFilter.Create(NewFilename);
    AFrmReports.QuickRep4.ExportToFilter(repPDF);
    //gtPDFDocument1.SaveToFile(NewFilename,naEnable);
    //LDoc.Free;
    ShellExecute(Handle, 'OPEN', PChar(NewFilename), nil, nil, SW_SHOW);
    AFrmReports.Free;

end;

end.
